// ////////////////////////////////////////////////////////////////////////////////////////
// NAME: PermissionsManager.java 
// AUTHOR: ??? 
// LAST MODIFIED: 28 March 2012 
// DESCRIPTION: Contains code for the Permissions manager responsible for stripping permissions from an application.
// LIMITATIONS: 
// CITATIONS: In code
// 
// ////////////////////////////////////////////////////////////////////////////////////////
package prototype.revision;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.concurrent.TimeoutException;

import android.util.Log;

import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.RootToolsException;

public class PermissionsManager {
	private final String WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";
	private final String SHUTDOWN = "android.permission.SHUTDOWN";
	private final String BROADCAST_WAP_PUSH = "android.permission.BROADCAST_WAP_PUSH";
	private final String WRITE_SMS = "android.permission.WRITE_SMS";
	private final String ACCESS_WIFI_STATE = "android.permission.ACCESS_WIFI_STATE";
	private final String ACCESS_COARSE_LOCATION = "android.permission.ACCESS_COARSE_LOCATION";
	private final String ACCESS_COUSRE_LOCATION = "android.permission.ACCESS_COARSE_LOCATION";
	private final String ACCESS_FINE_LOCATION = "android.permission.ACCESS_FINE_LOCATION";
	private final String STATUS_BAR = "android.permission.STATUS_BAR";
	private final String READ_CONTACTS = "android.permission.READ_CONTACTS";
	private final String CALL_PHONE = "android.permission.CALL_PHONE";
	private final String WRITE_CONTACTS = "android.permission.WRITE_CONTACTS";
	private final String BACKUP = "android.permission.BACKUP";
	private final String READ_PHONE_STATE = "android.permission.READ_PHONE_STATE";
	private final String READ_SMS = "android.permission.READ_SMS";
	private final String RECEIVE_BOOT_COMPLETED = "android.permission.RECEIVE_BOOT_COMPLETED";
	private final String STOP_APP_SWITCHES = "android.permission.STOP_APP_SWITCHES";
	private final String INTERNAL_SYSTEM_WINDOW = "android.permission.INTERNAL_SYSTEM_WINDOW";
	private final String BROADCAST_STICKY = "android.permission.BROADCAST_STICKY";
	private final String USE_SIP = "android.permission.USE_SIP";
	private final String RECORD_AUDIO = "android.permission.RECORD_AUDIO";
	private final String ACCESS_NETWORK_STATE = "android.permission.ACCESS_NETWORK_STATE";
	private final String WAKE_LOCK = "android.permission.WAKE_LOCK";
	private final String WRITE_APN_SETTINGS = "android.permission.WRITE_APN_SETTINGS";
	private final String WRITE_SECURE_SETTINGS = "android.permission.WRITE_SECURE_SETTINGS";
	private final String MODIFY_AUDIO_SETTINGS = "android.permission.MODIFY_AUDIO_SETTINGS";
	private final String DISABLE_KEYGUARD = "android.permission.DISABLE_KEYGUARD";
	private final String REORDER_TASKS = "android.permission.REORDER_TASKS";
	private final String SYSTEM_ALERT_WINDOW = "android.permission.SYSTEM_ALERT_WINDOW";
	private final String MODIFY_PHONE_STATE = "android.permission.MODIFY_PHONE_STATE";
	private final String INTERNET = "android.permission.INTERNET";
	private final String BLUETOOTH = "android.permission.BLUETOOTH";
	private final String WRITE_SETTINGS = "android.permission.WRITE_SETTINGS";
	private final String CHANGE_WIFI_STATE = "android.permission.CHANGE_WIFI_STATE";
	private final String BROADCAST_SMS = "android.permission.BROADCAST_SMS";
	private final String VIBRATE = "android.permission.VIBRATE";
	private final String SET_TIME_ZONE = "android.permission.SET_TIME_ZONE";
	private final String CHANGE_NETWORK_STATE = "android.permission.CHANGE_NETWORK_STATE";
	private final String BLUETOOTH_ADMIN = "android.permission.BLUETOOTH_ADMIN";
	private final String DEVICE_POWER = "android.permission.DEVICE_POWER";
	private final String CALL_PRIVILEGED = "android.permission.CALL_PRIVILEGED";
	private final String CHANGE_CONFIGURATION = "android.permission.CHANGE_CONFIGURATION";

	private Blocker blocker;
	private AppManager AM;

	protected void clean() {
		try {
			RootTools.sendShell("rm /data/system/packages1.xml", 1000);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RootToolsException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}

	}

	protected void exec(boolean block, String packageName, String permission) {
		final String path = "/data/system/packages.xml";
		final String path2 = "/data/system/packages1.xml";
		String perm = permission;

		try {
			RootTools
					.sendShell(
							"dd if=/data/system/packages.xml of=/data/system/packages1.xml",
							1000);
			Log.i("RootTools", "created packages1.xml..");
			RootTools.sendShell("chmod 0777 /data/system/packages.xml", 1000);
			Log.i("RootTools", "changed permissions packages.xml..");
			RootTools.sendShell("chmod 0777 /data/system/packages1.xml", 1000);
			Log.i("RootTools", "changed permissions packages.xml..");
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (RootToolsException e2) {
			e2.printStackTrace();
		} catch (TimeoutException e2) {
			e2.printStackTrace();
		}

		if (block == true) {
			System.out.println(packageName);
			try {
				Log.i("Try", "Blocked...true");
				LineNumberReader lnr = new LineNumberReader(new FileReader(
						path2));
				FileWriter fw = new FileWriter(path);
				String line;
				boolean flag = false;
				while ((line = lnr.readLine()) != null) {
					Log.i("WHile", "in the loop?.");
					if (line.contains(packageName)) {
						flag = true;
					}
					if (line.contains("<perms>") && flag) {
						Log.i("Tom", "XXXXXXXXXXXXXXXXXXXXXXXX");
						RootTools.debugMode = true;
						fw.write(line + "\n");
						Log.d("perm", line);
						while ((line = lnr.readLine()) != null) {
							if (line.contains("</perms>")) {
								break;
							}
							Log.d("perm", perm);
							Log.i("perm", line);
							if (!(line.contains(perm))) {
								fw.write(line + "\n");
							}
						}
					}
					if (line.contains("</shared-user>")) {
						flag = false;
					}
					fw.write(line + "\n");
				}
				fw.close();
				lnr.close();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				LineNumberReader lnr = new LineNumberReader(new FileReader(
						path2));
				FileWriter fw = new FileWriter(path);
				String line;
				boolean flag = false;
				while ((line = lnr.readLine()) != null) {
					if (line.contains(packageName)) {
						flag = true;
					}
					if (line.contains("<perms>") && flag) {
						RootTools.debugMode = true;
						fw.write(line + "\n");
						while ((line = lnr.readLine()) != null) {
							if (line.contains(perm)) {
								break;
							}
							if (line.contains("</perms>")) {
								fw.write("<item name=" + '"' + perm + '"'
										+ " />" + "\n");
								fw.write(line + "\n");
							} else {
								fw.write(line + "\n");
							}
						}
					}
					if (line.contains("</shared-user>")) {
						flag = false;
					}
					fw.write(line + "\n");
				}
				fw.close();
				lnr.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// clean();
	}
	
	protected boolean searchPermissions(String packageName, String permission) {
		final String path = "/data/system/packages.xml";
		final String path2 = "/data/system/packages1.xml";
		String perm = permission;

		try {
			RootTools
					.sendShell(
							"dd if=/data/system/packages.xml of=/data/system/packages1.xml",
							1000);
			Log.i("RootTools", "created packages1.xml..");
			RootTools.sendShell("chmod 0777 /data/system/packages.xml", 1000);
			Log.i("RootTools", "changed permissions packages.xml..");
			RootTools.sendShell("chmod 0777 /data/system/packages1.xml", 1000);
			Log.i("RootTools", "changed permissions packages.xml..");
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (RootToolsException e2) {
			e2.printStackTrace();
		} catch (TimeoutException e2) {
			e2.printStackTrace();
		}

			System.out.println(packageName);
			try {
				Log.i("Try", "Blocked...true");
				LineNumberReader lnr = new LineNumberReader(new FileReader(
						path2));
				String line;
				boolean flag = false;
				while ((line = lnr.readLine()) != null) {
					Log.i("WHile", "in the loop?.");
					if (line.contains(packageName)) {
						flag = true;
					}
					if (line.contains("<perms>") && flag) {
						Log.i("Tom", "XXXXXXXXXXXXXXXXXXXXXXXX");
						RootTools.debugMode = true;
						Log.d("perm", line);
						while ((line = lnr.readLine()) != null) {
							if (line.contains("</perms>")) {
								break;
							}
							Log.d("perm", perm);
							Log.i("perm", line);
							if ((line.contains(perm))) {
								return true;
							}
						}
					}
					if (line.contains("</shared-user>")) {
						flag = false;
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		// clean();
	}

	protected void block_all(boolean block, String permission) {
		final String path = "/data/system/packages.xml";
		final String path2 = "/data/system/packages1.xml";
		String perm = permission;
		String packageName;
		// read in Installed_Apps.txt
		FileOperations fo = new FileOperations();
		this.setBlocker(new Blocker(fo));
//		blocker = this.getBlocker();
		try {
			RootTools
					.sendShell(
							"dd if=/data/system/packages.xml of=/data/system/packages1.xml",
							1000);
			RootTools.sendShell("chmod 0777 /data/system/packages.xml", 1000);
			RootTools.sendShell("chmod 0777 /data/system/packages1.xml", 1000);
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (RootToolsException e2) {
			e2.printStackTrace();
		} catch (TimeoutException e2) {
			e2.printStackTrace();
		}

		if (block == true) {
			try {
				LineNumberReader lnr = new LineNumberReader(new FileReader(
						path2));
				FileWriter fw = new FileWriter(path);
				String line;
				boolean flag = false;
				while ((line = lnr.readLine()) != null) {
					for (int i = 0; i < fo
							.getNumberLinesInFile("installed_programs.txt"); i++) {
						if (line.contains(fo.readFileLine(
								"installed_programs.txt", i))) {
							fw.write(line + "\n");
							flag = true;
							while ((line = lnr.readLine()) != null) {
								if (line.contains(perm) && flag) {
									// black list
									packageName = fo.readFileLine(
											"installed_programs.txt", i);
									//fo.AppendFile(blocked, "black.txt");
									//blocker.addToBlacklist(packageName);
								} else {
									fw.write(line + "\n");
								}
								if (line.contains("<shared-user")) {
									flag = false;

								}

							}
						}
						fw.write(line + "\n");
					}
					fw.write(line + "\n");
				}
				fw.close();
				lnr.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				LineNumberReader lnr = new LineNumberReader(new FileReader(
						path2));
				FileWriter fw = new FileWriter(path);
				String line;
				while ((line = lnr.readLine()) != null) {
					if (line.contains("<shared-user")) {
						fw.write(line + "\n");
						for (int i = 0; i < fo
								.getNumberLinesInFile("black.txt"); i++) {
							if (line.contains(fo.readFileLine("black.txt", i))) {
								packageName = fo.readFileLine("black.txt", i);
								while ((line = lnr.readLine()) != null) {
									if (line.contains(perm)) {
										break;
									}
									if (line.contains("</perms>")) {
										fw.write("<item name=" + perm + " />"
												+ "\n");
										fw.write(line + "\n");
										//blocker.addToWhiteList(packageName);
									} else {
										fw.write(line + "\n");
									}
								}
							}
						}
					}
					fw.write(line + "\n");
				}
				fw.close();
				lnr.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// clean();
	}

	public String getCHANGE_CONFIGURATION() {
		return CHANGE_CONFIGURATION;
	}

	public String getWRITE_EXTERNAL_STORAGE() {
		return WRITE_EXTERNAL_STORAGE;
	}

	public String getSHUTDOWN() {
		return SHUTDOWN;
	}

	public String getBROADCAST_WAP_PUSH() {
		return BROADCAST_WAP_PUSH;
	}

	public String getWRITE_SMS() {
		return WRITE_SMS;
	}

	public String getACCESS_WIFI_STATE() {
		return ACCESS_WIFI_STATE;
	}

	public String getACCESS_COARSE_LOCATION() {
		return ACCESS_COARSE_LOCATION;
	}

	public String getSTATUS_BAR() {
		return STATUS_BAR;
	}

	public String getREAD_CONTACTS() {
		return READ_CONTACTS;
	}

	public String getCALL_PHONE() {
		return CALL_PHONE;
	}

	public String getWRITE_CONTACTS() {
		return WRITE_CONTACTS;
	}

	public String getBACKUP() {
		return BACKUP;
	}

	public String getREAD_PHONE_STATE() {
		return READ_PHONE_STATE;
	}

	public String getREAD_SMS() {
		return READ_SMS;
	}

	public String getRECEIVE_BOOT_COMPLETED() {
		return RECEIVE_BOOT_COMPLETED;
	}

	public String getSTOP_APP_SWITCHES() {
		return STOP_APP_SWITCHES;
	}

	public String getINTERNAL_SYSTEM_WINDOW() {
		return INTERNAL_SYSTEM_WINDOW;
	}

	public String getBROADCAST_STICKY() {
		return BROADCAST_STICKY;
	}

	public String getUSE_SIP() {
		return USE_SIP;
	}

	public String getRECORD_AUDIO() {
		return RECORD_AUDIO;
	}

	public String getACCESS_NETWORK_STATE() {
		return ACCESS_NETWORK_STATE;
	}

	public String getWAKE_LOCK() {
		return WAKE_LOCK;
	}

	public String getWRITE_APN_SETTINGS() {
		return WRITE_APN_SETTINGS;
	}

	public String getWRITE_SECURE_SETTINGS() {
		return WRITE_SECURE_SETTINGS;
	}

	public String getMODIFY_AUDIO_SETTINGS() {
		return MODIFY_AUDIO_SETTINGS;
	}

	public String getDISABLE_KEYGUARD() {
		return DISABLE_KEYGUARD;
	}

	public String getREORDER_TASKS() {
		return REORDER_TASKS;
	}

	public String getSYSTEM_ALERT_WINDOW() {
		return SYSTEM_ALERT_WINDOW;
	}

	public String getMODIFY_PHONE_STATE() {
		return MODIFY_PHONE_STATE;
	}

	public String getINTERNET() {
		return INTERNET;
	}

	public String getBLUETOOTH() {
		return BLUETOOTH;
	}

	public String getWRITE_SETTINGS() {
		return WRITE_SETTINGS;
	}

	public String getCHANGE_WIFI_STATE() {
		return CHANGE_WIFI_STATE;
	}

	public String getBROADCAST_SMS() {
		return BROADCAST_SMS;
	}

	public String getVIBRATE() {
		return VIBRATE;
	}

	public String getSET_TIME_ZONE() {
		return SET_TIME_ZONE;
	}

	public String getCHANGE_NETWORK_STATE() {
		return CHANGE_NETWORK_STATE;
	}

	public String getBLUETOOTH_ADMIN() {
		return BLUETOOTH_ADMIN;
	}

	public String getDEVICE_POWER() {
		return DEVICE_POWER;
	}

	public String getCALL_PRIVILEGED() {
		return CALL_PRIVILEGED;
	}

	public Blocker getBlocker() {
		return blocker;
	}

	public void setBlocker(Blocker blocker) {
		this.blocker = blocker;
	}

	public String getACCESS_COUSRE_LOCATION() {
		return ACCESS_COUSRE_LOCATION;
	}

	public String getACCESS_FINE_LOCATION() {
		return ACCESS_FINE_LOCATION;
	}
}